package api

import (
	"backend/internal/controller"
	"backend/internal/service"
	"github.com/gin-gonic/gin"
)

// 创建全局路由注册函数
func RegisterAllRoutes(router *gin.Engine, services *service.Manager) {
	// 创建证据组路由
	evidenceGroup := router.Group("/evidence")
	registerEvidenceRoutes(evidenceGroup, services.EvidenceService)

	// 创建资产组路由
	assertGroup := router.Group("/assert")
	registerAssertRoutes(assertGroup, services.AssertService)
}

// 证据合约路由注册
func registerEvidenceRoutes(router *gin.RouterGroup, evService *service.EvidenceService) {
	ctrl := controller.NewEvidenceController(evService)

	// 基础操作
	router.POST("/initialize", ctrl.Initialize)
	router.GET("", ctrl.GetAllEvidences)
	router.GET("/:id", ctrl.GetEvidence)

	// 锁操作
	router.POST("/lock/side", ctrl.CreateSideChainLock)
	router.POST("/lock/main", ctrl.CreateMainChainLock)
	router.POST("/unlock/:id", ctrl.VerifyAndUnlock)
	router.POST("/rollback/:id", ctrl.TimeoutRollback)

	// 数据操作
	router.POST("/data/side", ctrl.SubmitSideChainData)
	router.PUT("/confirm/:id", ctrl.ConfirmSync)

	// 批量操作
	router.GET("/unsynced", ctrl.GetUnsyncedRecords)
	router.POST("/sync", ctrl.BatchSyncEvidences) // 重命名为更清晰的端点
}

// 资产合约路由注册
func registerAssertRoutes(router *gin.RouterGroup, asService *service.AssertService) {
	ctrl := controller.NewAssertController(asService)

	// 基础操作
	router.POST("/mint", ctrl.Mint)
	router.GET("/balance", ctrl.GetAllTokenBalances)
	router.POST("/balance", ctrl.CreateTokenBalances)

	// 转账操作
	router.POST("/transfer", ctrl.AssertTransfer)

	// HTL操作
	router.POST("/htl/sender", ctrl.CreateSenderHTL)
	router.POST("/htl/receiver", ctrl.CreateReceiverHTL)
	router.POST("/unlock/sender/:id", ctrl.UnlockSenderHTL)
	router.POST("/unlock/receiver/:id", ctrl.UnlockReceiverHTL)
	router.POST("/refund/:id", ctrl.RefundHTL)

	// 证明管理
	router.GET("/proof", ctrl.GetAllProofs)
	router.POST("/proof/verify", ctrl.VerifyProof)
	router.POST("/proof/save/:id", ctrl.VerifyAndSaveProof)
}
